package com.zzt.blog.common.interceptor;

import com.zzt.blog.common.constant.Constants;
import com.zzt.blog.common.util.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;


/**
 * Created with IntelliJ IDEA.
 * Description
 * User: panda
 * Date: 2025-04-18
 * Time: 14:11
 */
@Component
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String userToken = request.getHeader(Constants.USER_TOKEN_HEADER_KEY);
        log.info("从header中获取token：",userToken);
        if(userToken == null){
            //拦截
            response.setStatus(401);
            return false;
        }
        //校验token是不是合法
        Claims claims = JwtUtils.parseToken(userToken);
        if(claims == null){
            //拦截
            response.setStatus(401);
            return false;
        }
        return true;
    }
}
